var app = getApp();
import QRCode from '../../utils/weapp-qrcode.js';
let animationShowHeight = 300;
Page({
  data: {
    loading:false,
    animation: '',
    list:[],
    display:"none",
    unpaid:"https://img.quanlaoba.com/skins/static/skin2295_zhcxmall_mobile/widgets/member-centre_php/unpaid.png",
    unreceived:"https://img.quanlaoba.com/skins/static/skin2295_zhcxmall_mobile/widgets/member-centre_php/unreceived.png",
    received:"https://img.quanlaoba.com/skins/static/skin2295_zhcxmall_mobile/widgets/member-centre_php/received.png",
    refund:"https://img.quanlaoba.com/skins/static/skin2295_zhcxmall_mobile/widgets/member-centre_php/refund.png"
  },

  onShow: function (options) {
    this.getUser();
  },

 
  getUser:function(e){
    let that=this;
    wx.showLoading({
      title: '加载中...',
    })
    app.getUserToken(function (err, token) {
      wx.request({
        url: app.globalData.url +'/index.php?_m=shop/customers/get_item',
        header: {
          'content-type': 'application/x-www-form-urlencoded',
        },
        method: 'POST',
        data: { auth_token: token },
        success: (res) => {
          wx.hideLoading();
          if (res.data.status == 401) {
            // 清空授权token
            app.globalData.token = null
            wx.removeStorageSync('token')
            that.getUser();
          } else if(res.data.code == 0){
            that.setData({
              loading: true,
              list: res.data.data.item
            })
            that.getCode(res.data.data.item.pickup_url);
          }
        }
      })
    })
  },

  getCode:function(url){
    
    new QRCode('canvas', {
      text: url,
      width: 200,
      height: 200,
      padding: 12, // 生成二维码四周自动留边宽度，不传入默认为0
      correctLevel: QRCode.CorrectLevel.L, // 二维码可辨识度
      callback: (res) => {
        console.log(res.path)
        // 接下来就可以直接调用微信小程序的api保存到本地或者将这张二维码直接画在海报上面去，看各自需求
      }
    })
  },

  showCord(){
    let display=this.data.display;
    let that=this;
    if(display == "none"){
      var animation = wx.createAnimation({
        transformOrigin: "50% 50%",
        duration: 1000,
        timingFunction: "ease",
        delay: 0
      })
      this.animation = animation
      animation.translateY(animationShowHeight).step()
      this.setData({
        animation: animation.export(),
      })
      setTimeout(function () {
        that.setData({
          display: "true",
        })
      }, 500)
    }else{
      var animation = wx.createAnimation({
        transformOrigin: "50% 50%",
        duration: 1000,
        timingFunction: "ease",
        delay: 0
      })
      this.animation = animation
      animation.translateY(0).step()
      this.setData({
        display: "none",
        animation: animation.export(),
      })
    }
    
  },

  hideCord(){
    var animation = wx.createAnimation({
      transformOrigin: "50% 50%",
      duration: 1000,
      timingFunction: "ease",
      delay: 0
    })
    this.animation = animation
    animation.translateY(0).step()
    this.setData({
      display:"none",
      animation: animation.export(),
    })
  },
  
  onPullDownRefresh: function () {
    wx.showToast({
      title: '加载中...',
      icon: 'loading'
    })

    // 初始化信息
    this.getUser();

    setTimeout(function () {
      wx.stopPullDownRefresh()
    }, 1000)

  },

})